/* $Id: ActionController.java 64 2010-02-14 13:53:27Z aaron.day $ */
package org.crazydays.gameplan.controller;


import org.crazydays.gameplan.action.ExitApplication;
import org.crazydays.gameplan.action.OpenAboutDialog;
import org.crazydays.gameplan.db.Database;
import org.crazydays.gameplan.db.action.CloseDatabase;
import org.crazydays.gameplan.db.action.CreateDatabase;
import org.crazydays.gameplan.db.action.OpenDatabase;
import org.crazydays.gameplan.db.action.SaveDatabase;
import org.crazydays.gameplan.db.action.SaveDatabaseAs;
import org.crazydays.gameplan.map.action.CreateMapDialog;
import org.crazydays.gameplan.map.action.ToggleGrid;
import org.crazydays.gameplan.model.IDatabaseObserver;
import org.crazydays.gameplan.model.Model;

/**
 * Actions
 */
public class ActionController
    implements IDatabaseObserver
{
    /** serial version uid */
    public final static long serialVersionUID =
        "$Id: ActionController.java 64 2010-02-14 13:53:27Z aaron.day $"
            .hashCode();

    /** model */
    protected Model model;

    /** create database */
    protected CreateDatabase createDatabase;

    /** open database */
    protected OpenDatabase openDatabase;

    /** close database */
    protected CloseDatabase closeDatabase;

    /** save database */
    protected SaveDatabase saveDatabase;

    /** save database as */
    protected SaveDatabaseAs saveDatabaseAs;

    /** create map dialog */
    protected CreateMapDialog createMapDialog;

    /** toggle grid */
    protected ToggleGrid toggleGrid;

    /** about */
    protected OpenAboutDialog openAboutDialog;

    /** exit */
    protected ExitApplication exitApplication;

    /**
     * Initialize actions.
     */
    public void initialize()
    {
        initializeState();

        model.addDatabaseObserver(this);
    }

    /**
     * Initialize state.
     */
    protected void initializeState()
    {
        closeDatabase.setEnabled(false);
        saveDatabase.setEnabled(false);
        saveDatabaseAs.setEnabled(false);
        toggleGrid.setEnabled(false);
        createMapDialog.setEnabled(false);
    }

    public void setModel(Model model)
    {
        this.model = model;
    }

    /**
     * Set create database.
     * 
     * @param createDatabase CreateDatabase
     */
    public void setCreateDatabase(CreateDatabase createDatabase)
    {
        this.createDatabase = createDatabase;
    }

    /**
     * Set open database.
     * 
     * @param openDatabase OpenDatabase
     */
    public void setOpenDatabase(OpenDatabase openDatabase)
    {
        this.openDatabase = openDatabase;
    }

    /**
     * Set close database action.
     * 
     * @param closeDatabase CloseDatabase
     */
    public void setCloseDatabase(CloseDatabase closeDatabase)
    {
        this.closeDatabase = closeDatabase;
    }

    /**
     * Set save database.
     * 
     * @param saveDatabase SaveDatabase
     */
    public void setSaveDatabase(SaveDatabase saveDatabase)
    {
        this.saveDatabase = saveDatabase;
    }

    /**
     * Set save database as.
     * 
     * @param saveDatabaseAs SaveDatabaseAs
     */
    public void setSaveDatabaseAs(SaveDatabaseAs saveDatabaseAs)
    {
        this.saveDatabaseAs = saveDatabaseAs;
    }

    /**
     * Set create map dialog.
     * 
     * @param createMapDialog CreateMapDialog
     */
    public void setCreateMapDialog(CreateMapDialog createMapDialog)
    {
        this.createMapDialog = createMapDialog;
    }

    /**
     * Set toggle grid.
     * 
     * @param toggleGrid ToggleGrid
     */
    public void setToggleGrid(ToggleGrid toggleGrid)
    {
        this.toggleGrid = toggleGrid;
    }

    /**
     * Get toggle grid.
     * 
     * @return ToggleGrid
     */
    public ToggleGrid getToggleGrid()
    {
        return toggleGrid;
    }

    /**
     * Set open about dialog.
     * 
     * @param openAboutDialog OpenAboutDialog
     */
    public void setOpenAboutDialog(OpenAboutDialog openAboutDialog)
    {
        this.openAboutDialog = openAboutDialog;
    }

    /**
     * Set exit application.
     * 
     * @param exitApplication ExitApplication
     */
    public void setExitApplication(ExitApplication exitApplication)
    {
        this.exitApplication = exitApplication;
    }

    /**
     * Database is opened.
     * 
     * @param database Database
     * @see IDatabaseObserver#opened(Database)
     */
    @Override
    public void opened(Database database)
    {
        createDatabase.setEnabled(false);
        openDatabase.setEnabled(false);
        saveDatabase.setEnabled(true);
        saveDatabaseAs.setEnabled(true);
        createMapDialog.setEnabled(true);
        closeDatabase.setEnabled(true);
    }

    /**
     * Database is closed.
     * 
     * @param database Database
     * @see IDatabaseObserver#closed(Database)
     */
    @Override
    public void closed(Database database)
    {
        createDatabase.setEnabled(true);
        openDatabase.setEnabled(true);
        saveDatabase.setEnabled(false);
        saveDatabaseAs.setEnabled(false);
        createMapDialog.setEnabled(false);
        closeDatabase.setEnabled(false);
    }

    /**
     * Enable map options.
     */
    public void enableMapOptions()
    {
        toggleGrid.setEnabled(true);
    }

    /**
     * Disable map options.
     */
    public void disableMapOptions()
    {
        toggleGrid.setEnabled(false);
    }
}
